import paho.mqtt.client as mqtt


class MqttClient():
    def __init__(self):
        self.host = '120.78.168.67'
        self.port = '1883'

    def on_connect(self, client, userdata, flags, rc):
        pass

    def publish(self, topic, message):
        """发布消息"""
        client = mqtt.Client()
        client.on_connect = self.on_connect
        client.connect(self.host, 1883, 60)
        # 四个参数分别为：主题，发送内容，QoS, 是否保留消息
        client.publish(topic, payload=message, qos=0, retain=False)


# test = MqttClient()
# test.publish('test', 'aaaaa')